<?php

require_once('lib/calcDateTime.class.php');

class Vliegfabriek {

    private $user = "vliegticketsgoedkoop";
    private $userKey = "4JH83";
    private $baseUrl = "https://www.vliegfabriek.nl/api/xml/flight/availability/";
    private $search;
    private $tickets = array();

    public function Vliegfabriek($searchObj) {
        $this->search = $searchObj;
        $this->buildUrl();
    }

    public function getTickets($index) {

        $use_errors = libxml_use_internal_errors(true);
        $xml = simplexml_load_string($this->newHttpRequest($this->baseUrl));
        if (!$xml) {
            //throw new Exception("Cannot load xml source.\n");
        }
        libxml_clear_errors();
        libxml_use_internal_errors($use_errors);

        if ($xml != null) {
            foreach ($xml->resource as $flight) {

                $stops = count($flight->flights_outbound->resource->resource) - 1;

                //uren en minuten uitrekenen
                $journeyTime = $this->ConvertMinutes2Hours((int) $flight->flights_outbound->resource->resource->journey_time);

                $ticket = new Ticket($index, (string) $flight->select_url,
                                (double) $flight->total_price,
                                (double) $flight->total_price / $this->search->totalPassengers(),
                                (string) $flight->operating_carrier,
                                (double) $flight->total_ticketfee,
                                $stops,
                                $journeyTime);

                foreach ($flight->flights_outbound->resource->resource as $outbound) {

                    $departureDate = new DateTime($outbound->departure->date . $outbound->departure->time);
                    $arrivalDate = new DateTime($outbound->arrival->date . $outbound->arrival->time);

                    $ticket->setFlightOutbound((string) $outbound->departure->airport, (string) $outbound->arrival->airport, $departureDate, $arrivalDate, (string) $outbound->carrier, $this->ConvertMinutes2Hours($outbound->flight_time), (string) $outbound->flight_number);
                }
                
                if(count($flight->flights_inbound->resource->resource) != null){
                    foreach ($flight->flights_inbound->resource->resource as $inbound) {

                        $departureDate = new DateTime($inbound->departure->date . $inbound->departure->time);
                        $arrivalDate = new DateTime($inbound->arrival->date . $inbound->arrival->time);

                        $ticket->setFlightInbound((string) $inbound->departure->airport, (string) $inbound->arrival->airport, $departureDate, $arrivalDate, (string) $inbound->carrier, $this->ConvertMinutes2Hours($inbound->flight_time), (string) $inbound->flight_number);
                    }
                }
                
                $this->tickets[$index] = $ticket;
                $index = $index + 1;
            }
        }
        //print_r($this->tickets);
        return $this->tickets;
    }

    private function newHttpRequest($requestUrl) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $requestUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

        curl_setopt($ch, CURLOPT_USERPWD, "$this->user:$this->userKey");
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }

    private function buildUrl() {
        if ($this->search->getRetour()) {
            $flightType = "?search_type=return";
        } else {
            $flightType = "?search_type=oneway";
        }

        $deptAirport = "&departure_airport=" . $this->search->getDeparturyCityIATA();
        $deptDate = "&departure_date=" . $this->search->getDepartureDate();
        $arrAirport = "&destination_airport=" . $this->search->getDestinationCityIATA();

        //Minimum URL
        $this->baseUrl = $this->baseUrl . $flightType . $deptAirport . $deptDate . $arrAirport;

        if ($this->search->getRetour())
            $this->baseUrl = $this->baseUrl . "&return_date=" . $this->search->getReturnDate();
        if ($this->search->getAdults() > 0)
            $this->baseUrl = $this->baseUrl . "&nr_of_adults=" . $this->search->getAdults();
        if ($this->search->getChildren() > 0)
            $this->baseUrl = $this->baseUrl . "&nr_of_children=" . $this->search->getChildren();
        if ($this->search->getInfants() > 0)
            $this->baseUrl = $this->baseUrl . "&nr_of_infants=" . $this->search->getInfants();

        return $this->baseUrl;
    }

    private static function ConvertMinutes2Hours($Minutes) {
        if ($Minutes < 0) {
            $Min = Abs($Minutes);
        } else {
            $Min = $Minutes;
        }
        $iHours = Floor($Min / 60);
        $Minutes = ($Min - ($iHours * 60)) / 100;
        $tHours = $iHours + $Minutes;
        if ($Minutes < 0) {
            $tHours = $tHours * (-1);
        }
        $aHours = explode(".", $tHours);
        $iHours = $aHours[0];
        if (empty($aHours[1])) {
            $aHours[1] = "00";
        }
        $Minutes = $aHours[1];
        if (strlen($Minutes) < 2) {
            $Minutes = $Minutes . "0";
        }
        $tHours = $iHours . ":" . $Minutes;
        return $tHours;
    }

}